package com.Leetcode;

import com.structure.TreeNode;

public class Index114Flatten {
	public void flatten(TreeNode root) {
		if(root == null) return;
		connectTreeNode(root);
    }
	
	TreeNode connectTreeNode(TreeNode root) {
		if(root == null) return null;
		connectTreeNode(root.left);
		connectTreeNode(root.right);
		
		TreeNode leftNode = root.left;
		TreeNode rightNode = root.right;
		root.left = null;
		root.right = leftNode;
		
		while(leftNode.right != null)
			leftNode = leftNode.right;
		leftNode.right = rightNode;
		return root;
	}
}
